package controllers

import (
	"encoding/json"
	"hapi/models"
	"strconv"
	// "fmt"
	"github.com/astaxie/beego"
)

// Operations about RequestTokens
type RequestTokenController struct {
	beego.Controller
}

// @Title RequestToken
// @Description Logs user into the system
// @Param	body		body 	models.RequestToken	true		"body for request token content"
// @Success 200 {string} models.RequestToken.Reg_key
// @Failure 403 body is empty
// @router / [post]
func (u *RequestTokenController) RequestToken() {

	var token models.RequestToken
	json.Unmarshal(u.Ctx.Input.RequestBody, &token)

	tokenstr, _ := models.AddRequestToken(token)
	u.Data["json"] = map[string]string{"token": tokenstr}
	u.Ctx.ResponseWriter.Header().Add("Access-Control-Allow-Origin", "*")
	u.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	// u.Ctx.ResponseWriter.Header().Add("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept")
	u.ServeJSON()
}

// @Title GetAll
// @Description get all RequestTokens
// @Success 200 {object} models.RequestToken
// @router / [get]
func (u *RequestTokenController) GetAll() {
	u.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	tokens := models.GetAllRequestTokens()
	u.Data["json"] = tokens
	u.ServeJSON()
}

// @Title DeleteRequestToken
// @Description delete a specific RequestToken
// @Success 200 {object} models.RequestToken
// @router /:objectId [delete]
func (u *RequestTokenController) DeleteRequestToken() {
	u.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	objectId := u.Ctx.Input.Param(":objectId")
	id, _ := strconv.Atoi(objectId)  
    models.DeleteRequestToken(id)
	u.Data["json"] = "Success"
	u.ServeJSON()
}
